<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="护理名称" prop="itemName">
            <el-input v-model="formData.itemName" placeholder="请输入护理名称" :clearable="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="费用" prop="itemFee">
            <InputNumber v-model="formData.itemFee" placeholder="请输入费用" type="decimal" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="所需时间（分钟）" prop="itemTime">
            <InputNumber v-model="formData.itemTime" placeholder="请输入所需时间（分钟）" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="简介" prop="itemIntroduction">
            <el-input
              v-model="formData.itemIntroduction"
              placeholder="请输入简介"
              :clearable="true"
              type="textarea"
              :rows="3"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图片" prop="imgUrl">
            <UploadImg v-model="formData.imgUrl" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="NurseItemEdit" lang="ts">
import * as NurseItemApi from '@/api/nurse/nurseItem'
import * as NurseItemModel from '@/model/nurse/item/NurseItem'
const { t } = useI18n()
const message = useMessage()
let formType = ''
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  id: '',
  itemName: '',
  itemFee: 0,
  itemTime: 0,
  itemIntroduction: '',
  imgUrl: ''
})
const formRules = reactive({
  itemName: [{ required: true, message: '请输入护理名称', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await NurseItemApi.get(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: '',
    itemName: '',
    itemFee: 0,
    itemTime: 0,
    itemIntroduction: '',
    imgUrl: ''
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as NurseItemModel.NurseItem
    if (formType === 'create') {
      await NurseItemApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await NurseItemApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
